const chalk = require("chalk");

module.exports = (program, config) => {
  program
    .version(config.version) // 当你执行  -V 命令的时候，就会显示：1.0.0
    .usage("<command> [options]")
    .command("create <project-name>", "create a new project")
    .command("create-pc <project-name>", "create a new PC project")
    .command("create-mobile <project-name>", "create a new mobile project")
    .command("create-uniapp <project-name>", "create a new uniapp project")
    .command("create-taro <project-name>", "create a new taro project")
    .command("create-noui <project-name>", "create a new no ui project")
    .option("-r, --registry <url>", "registration source", config.LOCALSOURCE)
    .option("-f, --force", "force overwrite of existing directory (if any)")
    .option("-d, --debug", "program debugging, internal prompt")
    .option("-c, --config <profile>", "use custom template profile")
    .option("-l, --language <language-name>", "use language type")
    .option("-p, --langpack <url>", "use custom language pack")
    .option("-n, --ignore <url>", "use the ignore step list")
    .action(function () {
      let options = false;
      if (arguments.length === 2) {
        options = arguments[1];
      } else {
        options = arguments[2];
      }
      if (options && options.registry) {
        process.env.LOCALSOURCE = options.registry;
      }
      if (options && options.force) {
        process.env.FORCEOVERWRITE = options.force;
      } else {
        process.env.FORCEOVERWRITE = "false";
      }
      if (options && options.config) {
        process.env.CUSTOMCONFIG = options.config;
      }
      if (options && options.language) {
        process.env.LANGUAGE = options.language;
      }
      if (options && options.debug) {
        process.env.DEBUG = options.debug;
      } else {
        process.env.DEBUG = "false";
      }
      if (options && options.langpack) {
        process.env.LANGPACK = options.langpack;
      }
      if (options && options.ignore) {
        process.env.IGNORE = options.ignore;
      }
    });

  program
    .command("init <project-name>")
    .description("initialize the package.json file")
    .action((cmd, options) => {
      require("../init/index")(cmd, options);
    });

  program
    .command("upgrade <pkg-name>")
    .description("upgrade the latest version of the npm dependency package")
    .option("-u, --update <version>", "upgrade to a specific version")
    .action((cmd, options) => {
      require("../update")(cmd, options);
    });

  program
    .command("pwa <project-name>")
    .description("configure pwa")
    .option("-t, --to <dir-name>", "configure to the specified directory")
    .action((cmd, options) => {
      require("../pwa")(cmd, options);
    });

  program
    .command("register <source-url>")
    .description("register global source")
    .action((cmd, options) => {
      require("../register")(cmd, options);
    });

  program
    .command("info")
    .description("print debugging information about your environment")
    .action((cmd) => {
      console.log(chalk.bold("\nEnvironment Info:"));
      require("envinfo")
        .run(
          {
            System: ["OS", "CPU"],
            Binaries: ["Node", "Yarn", "npm"],
            Browsers: ["Chrome", "Edge", "Firefox", "Safari"],
            npmPackages: [
              "typescript",
              "vue",
              "react",
              "webpack",
              "vite",
              "taro",
            ],
            npmGlobalPackages: ["mfo", "@vue/cli", "@tarojs/cli"],
            IDEs: ["VSCode"],
          },
          {
            showNotFound: false,
            duplicates: true,
            fullTree: true,
          }
        )
        .then(console.log);
    });

  program.parse(process.argv);

  const proc = program.runningCommand;

  if (proc) {
    proc.on("close", process.exit.bind(process));
    proc.on("error", () => process.exit(1));
  }

  const subCmd = program.args[0];
  if (!subCmd) {
    program.help();
  }
};
